const { Db } = require('mongodb');

var mongoClient = require('mongodb').MongoClient;

var address = "mongodb://localhost:27017/";
var collection = 'runoob';

var url = address + collection;

// console.log(url);

mongoClient.connect(url, function(err, db) {

    if (err) {
        console.log(err);
        return;
    }
    console.log('数据库已连接！');

    var obj = {name : "菜鸟教程", url: 'www.runoob.com'};
    var whereStr = {name: "菜鸟教程"};
    var updateStr = {$set: {'url': 'https://www.runoob.com/'}};

    var dbase = db.db(collection);
    try {
        // 创建集合
        // createCollection(dbase, collection);
    } catch (error) {
        console.warn(error);
    }
 
    // 插入数据
    // insertDocument(dbase, collection, obj);

    // 更新数据
    // updateManyByCondition(dbase, collection, whereStr, updateStr);

    // 查询数据
    // whereStr = {};
    // findByCondition(dbase, collection, whereStr);

    // 删除数据
    // deleteManyByCondition(dbase, collection, whereStr);

    // findByCondition(dbase, collection, whereStr);
   
    // 排序数据
    whereStr = {};
    sortStr = {name: 1};
    sortByCondition(dbase, collection, whereStr, sortStr);

    db.close();
});

/**
 * 创建集合
 * @param {*} dbase 
 * @param {*} collection 
 */
function createCollection(dbase, collection) {
    dbase.createCollection(collection, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log('创建集合' + collection + "成功！");
    });
}

/**
 * 添加一条数据
 * @param {} dbase 
 * @param {*} collection 
 * @param {*} obj 
 */
function insertDocument(dbase, collection, obj) {
    dbase.collection(collection).insertOne(obj, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log("文档插入成功");
    });
}

/**
 * 查询数据 通过条件
 * @param {*} dbase 
 * @param {*} collection 
 * @param {*} condition 
 */
function findByCondition(dbase, collection, whereStr) {

    dbase.collection(collection).find(whereStr).toArray(function(err, result) {

        if (err) {
            console.log(err);
            return;
        }
        console.log(result);
    });
}

/**
 * 更新一条数据
 * @param {*} dbase 
 * @param {*} collection 
 * @param {*} whereStr 
 * @param {*} updateStr 
 */
function updateOneByCondition(dbase, collection, whereStr, updateStr) {

    dbase.collection(collection).updateOne(whereStr, updateStr, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log(res.result.nModified + "条文档更新成功");
    })
}

/**
 * 更新多条数据
 * @param {*} dbase 
 * @param {*} collection 
 * @param {*} whereStr 
 * @param {*} updateStr 
 */
function updateManyByCondition(dbase, collection, whereStr, updateStr) {

    dbase.collection(collection).updateMany(whereStr, updateStr, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log(res.result.nModified + "条文档更新成功");
    })
}

/**
 * 删除一条数据
 * @param {*} dbase 
 * @param {*} collection 
 * @param {*} whereStr 
 */
function deleteOneByCondition(dbase, collection, whereStr) {

    dbase.collection(collection).deleteOne(whereStr, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log("文档删除成功！");
    });
}

/**
 * 删除多条数据
 * @param {*} dbase 
 * @param {*} collection 
 * @param {*} whereStr 
 */
function deleteManyByCondition(dbase, collection, whereStr) {

    dbase.collection(collection).deleteMany(whereStr, function(err, res) {
        if (err) {
            console.log(err);
            return;
        }
        console.log("文档删除成功！");
    });
}

/**
 * 排序数据
 * @param {*} dbase 
 * @param {*} collection 
 * @param {*} whereStr 
 * @param {*} sortStr 
 */
function sortByCondition(dbase, collection, whereStr, sortStr) {

    dbase.collection(collection).find(whereStr).sort(sortStr).toArray(function(err, result) {
        if (err) {
            console.log(err);
            return;
        }
        console.log(result);
    });
}